/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.util;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.iwindplus.base.domain.constant.CommonConstant;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 唯一key工具类.
 *
 * @author zengdegui
 * @since 2024/07/06 12:39
 */
public class KeysUtil extends KeyUtil {

    /**
     * 拼接生成key.
     *
     * @param md5Enabled 是否md5加密
     * @param target     类
     * @param method     方法
     * @param params     参数
     * @return String
     */
    public static String generate(Boolean md5Enabled, Object target, Method method, Object... params) {
        StringBuilder sb = new StringBuilder(target.getClass().getName())
            .append(CommonConstant.SymbolConstant.COLON)
            .append(method.getName());
        if (ArrayUtil.isNotEmpty(params)) {
            sb.append(CommonConstant.SymbolConstant.WELL_NO);
            String param = Arrays.stream(params).filter(Objects::nonNull)
                .map(Objects::toString).collect(Collectors.joining(CommonConstant.SymbolConstant.UNDERLINE));
            sb.append(param);
        }
        if (Boolean.TRUE.equals(md5Enabled)) {
            return DigestUtil.md5Hex(sb.toString());
        }
        return sb.toString();
    }
}
